<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              @cell-dblclick="getDatachou"  style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="userId" label="ID"  width="60" >
      </el-table-column>
      <el-table-column prop="username" label="姓名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="password" label="密码" width="120" sortable>
      </el-table-column>
      <el-table-column prop="alias" label="别名" width="100"  sortable>
      </el-table-column>
      <el-table-column prop="dept.deptName" label="部门" width="100"  sortable>
      </el-table-column>
      <el-table-column prop="createdBy" label="创建人" width="100"  sortable>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100"  sortable>
        <template scope="scope">
          <span style="color: #6aff00" v-if="scope.row.status ===1">开启</span>
          <span style="color: red" v-else-if="scope.row.status ===0">禁用</span>
          <span style="color: #ffd500" v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间"  sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :current-page="currentPage" :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="editForm.username" auto-complete="off"></el-input>
          <el-input v-model="editForm.userId" type="hidden"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="editForm.password" ></el-input>
        </el-form-item>
        <el-form-item label="别名" prop="alias">
          <el-input v-model="editForm.alias" ></el-input>
        </el-form-item>
        <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="editForm.createdBy" ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="editForm.status">
            <el-radio class="radio" :label="1">开启</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="部门" prop="deptId">
          <el-input v-model="editForm.deptId" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--新增界面-->
    <el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="addForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="别名" prop="alias">
          <el-input v-model="addForm.alias" ></el-input>
        </el-form-item>
        <el-form-item label="创建人" prop="createdBy">
          <el-select v-model="addForm.createdBy" placeholder="请选择">
            <el-option
                v-for="item in manager"
                :label="item.username"
                :value="item.username">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.userId }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="addForm.status">
            <el-radio class="radio" :label="1">开启</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="部门" prop="deptId">
          <el-input v-model="addForm.deptId" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--抽屉-->
    <el-drawer
        title="这是一个抽屉!"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-table :data="gridData">
        <el-table-column property="userId" label="用户ID" width="150"></el-table-column>
        <el-table-column property="username" label="姓名" width="200"></el-table-column>
        <el-table-column property="createtime" label="创建时间"></el-table-column>
      </el-table>
    </el-drawer>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import {getUserListPage, removeUser, batchRemoveUser, editUser, addUser} from '../../api/api';

export default {
  data() {
    return {
      table:false,
      gridData:[],
      filters: {
        keyword: ''
      },
      users: [],
      value: '',
      manager:[],
      total: 0,
      pageSize: 10,
      currentPage:1,
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        username: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      //编辑界面数据
      editForm: {
        userId: '',
        username: '',
        password:'',
        alias: '',
        deptId: '',
        createdBy: '',
        status: 0
      },

      addFormVisible: false,//新增界面是否显示
      addLoading: false,
      addFormRules: {
        username: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      //新增界面数据
      addForm: {
        username: '',
        alias: '',
        deptId: '',
        createdBy: '',
        status: 0
      }

    }
  },
  methods: {
    getDatachou(row) {
      this.table=true;
      let e= row.deptId;
      console.log(typeof e)
      this.$http.get("/user/deptuser/"+e).then(result=>{
        console.log(result.data)
          this.gridData=result.data
      }).catch({

      })
    },
    //查询所有用户
    getManager(){
      this.$http.get("/user").then(result=>{
        this.manager=result.data;
        console.log(result.data)
      }).catch(result=>{

      })
    },
    search(){
    this.currentPage=1,
        this.getUsers()
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getUsers();
    },
    //获取用户列表
    getUsers() {
      let para = {
        pageSize: this.pageSize,
        currentPage:this.currentPage,
        keyword: this.filters.keyword
      };
      this.listLoading = true;
      this.$http.post("/user", para).then(result => {
        this.listLoading=false
        console.log(result)
        this.users=result.data.rows
        this.total=result.data.total
      }).catch(result => {
        this.$message({
          message: '网络异常，请稍后！！',
          type: 'error'
        })
      })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = {userId: row.userId};
        this.$http.delete("/user/"+para.userId).then(result=>{
          console.log(para);
          this.listLoading=false;
          this.getUsers();
        })

      }).catch(() => {

      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.addFormVisible = true;
      this.addForm = {
        username: '',
        alias: '',
        deptId: '',
        createdBy: '',
        status: 0
      };
    },
    //编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.editForm);
            //发送axios请求
            this.$http.put("/user",para).then(result=>{
              //修改成功关闭忙等
              this.editLoading= false,
              this.editFormVisible = false;
              this.getUsers();
            }).catch(result=>{
              this.$message({
                message: '删除失败',
                type: 'error'
              });
              this.$refs['editForm'].resetFields();
              this.editFormVisible = false;
              this.getUsers();
            });
          });
        }
      });
    },
    //新增
    addSubmit: function () {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.addForm);
            //发送axios请求
            this.$http.put("/user",para).then(result=>{
              //添加成功关闭忙等
              this.addLoading= false,
              this.addFormVisible = false;
              this.getUsers();
              console.log(para)
              para=Object.assign({}, {});
            }).catch(result=>{
              this.$message({
                message: '添加失败',
                type: 'error'
              })
              this.$refs['addForm'].resetFields();
              this.addFormVisible = false;
              this.getUsers();
            });
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.userId)
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        //let para = {ids: ids};
       this.$http.patch("/user",ids).then(result=>{
         console.log(ids)
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getUsers();
        });
      }).catch(() => {
        this.$message({
          message: '删除失败',
          type: 'success'
        });
        this.getUsers();
      });
    }

  },
  mounted() {
    this.getUsers();
    this. getManager();
  }
}

</script>

<style scoped>

</style>